package com.xzl.org.learning_situation_analysis.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xzl.org.learning_situation_analysis.entity.*;
import com.xzl.org.learning_situation_analysis.response.RestResponse;
import com.xzl.org.learning_situation_analysis.service.*;
import com.xzl.org.learning_situation_analysis.util.CsvUtil;
import com.xzl.org.learning_situation_analysis.util.GenericUtil;
import com.xzl.org.learning_situation_analysis.util.MessageUtils;
import com.xzl.org.learning_situation_analysis.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 夏智力
 * @FileName ManageController
 * @date 2022/4/19 14:23
 * @Software: IntelliJ IDEA
 */

@RestController
@Slf4j
public class ManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private GradeService gradeService;

    @Autowired
    private QuestionScoreService questionScoreService;

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private ExamSessionService examSessionService;

    @Autowired
    private QuestionTypeService questionTypeService;

    @Transactional
    @PostMapping("/insertStudent")
    public RestResponse insertStudent(@RequestBody MultipartFile file) throws Exception {

        //解析CSv文件
        List<User> userList = CsvUtil.getCsvDataMethod3(file, User.class);
        Result<JSONObject> result = new Result<JSONObject>();

        for (User user: userList) {
            user.setSalt(GenericUtil.generateUUID().substring(0,5));
            user.setPassword(GenericUtil.md5(user.getPassword() + user.getSalt()));
            userService.insertUser(user);
            System.out.println(user);
        }

        return MessageUtils.success(result.getResult());
    }

    @Transactional
    @PostMapping("/insertQuestionScore")
    public  RestResponse insertQuestionScore(@RequestBody MultipartFile file) {

        List<QuestionScore> questionScoreList = CsvUtil.getCsvDataMethod3(file, QuestionScore.class);
        Result<JSONObject> result = new Result<JSONObject>();

        for (QuestionScore questionScore : questionScoreList) {
            questionScoreService.insertQuestionScore(questionScore);
        }
        return MessageUtils.success(result.getResult());
    }


    // 分页查询学生
    @GetMapping("/getAllUsers/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<User> getAllUser(@PathVariable("currentPage") Integer currentPage,
                                      @PathVariable("pageSize") Integer pageSize) {
        System.out.println(pageSize);
        PageInfo<User> currentPageUsers = userService.selectCurrentPageUser(currentPage, pageSize);
        return currentPageUsers;
    }

    // 分页查询班级
    @GetMapping("/getAllClazz/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Clazz> getAllClazz(@PathVariable("currentPage") Integer currentPage,
                                       @PathVariable("pageSize") Integer pageSize){
        System.out.println(pageSize);
        PageInfo<Clazz> currentPageClazz = clazzService.selectCurrentPageClazz(currentPage, pageSize);
        return currentPageClazz;
    }

    @PostMapping("/insertClazz")
    @ResponseBody
    public void insertClazz(@RequestBody Clazz clazz){
        clazzService.insertClazz(clazz);
    }

    // 分页查询考试
    @GetMapping("/getAllExam/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<ExamSession> getAllExam(@PathVariable("currentPage") Integer currentPage,
                                             @PathVariable("pageSize") Integer pageSize){
        System.out.println(pageSize);
        PageInfo<ExamSession> currentPageExam = examSessionService.selectCurrentPageExamSession(currentPage, pageSize);
        return currentPageExam;
    }

    @PostMapping("/insertExam")
    @ResponseBody
    public void insertExam(@RequestBody ExamSession examSession){
        examSessionService.insertExamSession(examSession);
    }

    // 分页查询年级
    @GetMapping("/getAllGrade/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Grade> getAllGrade(@PathVariable("currentPage") Integer currentPage,
                                            @PathVariable("pageSize") Integer pageSize){
        System.out.println(pageSize);
        PageInfo<Grade> currentPageGrade =gradeService.selectCurrentPageGrade(currentPage, pageSize);
        return currentPageGrade;
    }

    @PostMapping("/insertGrade")
    @ResponseBody
    public void insertGrade(@RequestBody Grade grade){
        gradeService.insertGrade(grade);
    }

    // 分页查询学科
    @GetMapping("/getAllSubject/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Subject> getAllSubject(@PathVariable("currentPage") Integer currentPage,
                                       @PathVariable("pageSize") Integer pageSize){
        System.out.println(pageSize);
        PageInfo<Subject> currentPageSubject = subjectService.selectCurrentPageSubject(currentPage, pageSize);
        return currentPageSubject;
    }

    @PostMapping("/insertSubject")
    @ResponseBody
    public void insertSubject(@RequestBody Subject subject){
        subjectService.insertSubject(subject);
    }
}
